﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Diagnostics.Contracts;
using ShredTheLog.Api;
using ShredTheLog.Api.DataContracts.TextChat;
using System.Globalization;

namespace ShredTheLog.Model
{
    public abstract class UserUpdateBase : ITextChatUpdate, IRoomUserUpdate, IUserInfo
    {
        [XmlAttribute("UID")]
        public int Uid { get; set; }

        [XmlAttribute("R")]
        public string RoomName { get; set; }
    }

    public sealed class AddUserUpdate : ShortUserInfo, ITextChatUpdate, IRoomUserUpdate
    {
        [XmlAttribute("R")]
        public string RoomName { get; set; }
    }

    public sealed class DropUserUpdate : UserUpdateBase
    {
    }

    public sealed class FlashPenUpdate : UserUpdateBase
    {
    }

    public sealed class PrivateChatRequestUpdate : ITextChatUpdate, IUserInfo
    {
        [XmlAttribute("UID")]
        public int Uid { get; set; }
    }

    public sealed class SysMessageUpdate : SysMessage, ITextChatUpdate
    {
    }

    public sealed class Message : IMessage
    {
        [XmlElement("uN")]
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value != null ? value.TrimEnd(' ', '·') : null; }
        }
        private string _firstName;

        [XmlIgnore]
        public bool IsMyMessage { get; set; }

        [XmlElement("mO")]
        public string Text { get; set; }

        public override string ToString()
        {
            if (String.IsNullOrEmpty(FirstName))
            {
                if (String.IsNullOrEmpty(Text))
                    return base.ToString();
                else
                    return Text;
            }
            else
            {
                if (String.IsNullOrEmpty(Text))
                    return FirstName;
                else
                    return String.Format(CultureInfo.CurrentCulture, "{0}: {1}", FirstName, Text);
            }
        }
    }

    [XmlRoot("ER")]
    public sealed class EnterRoomUpdate : ITextChatUpdate, IEnterRoomUpdate
    {
        [XmlAttribute("R")]
        public string RoomName { get; set; }

        private static readonly UserInfo[] EmptyUsers = new UserInfo[0];
        private UserInfo[] _users = EmptyUsers;
        [XmlElement("NU")]
        public UserInfo[] Users
        {
            get { return _users; }
            set { _users = value ?? EmptyUsers; }
        }

        private static readonly Message[] EmptyMessages = new Message[0];
        private Message[] _messages = EmptyMessages;
        [XmlElement("M")]
        public Message[] Messages
        {
            get { return _messages; }
            set { _messages = value ?? EmptyMessages; }
        }

        IUserInfo[] IEnterRoomUpdate.Users { get { return Users; } }
        
        IMessage[] IEnterRoomUpdate.Messages { get { return Messages; } }
    }

    public sealed class AddMessageUpdate : UserUpdateBase, IAddMessageUpdate
    {
        [XmlText]
        public string Text { get; set; }
    }
}
